﻿using Biz;
using BusinessLayer.Dal;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Biz
{
    public class ProductImage
    {
        [Key, Required, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [StringLength(250), Required]
        public string Image { get; set; }

        public virtual Product Product { get; set; }

        public static List<ProductImage> GetImagesOf(Product p)
        {
            return ProductImageDal.GetImagesOf(p);
        }

        public override string ToString()
        {
            return Image;
        }

        public static void DeleteAllImagesOf(Product p)
        {
            foreach (var x in DatabaseContext.DB.ProductImages.Where(pi => pi.Product.Id == p.Id).ToList())
            {
                DatabaseContext.DB.ProductImages.Remove(x);
            }
            DatabaseContext.DB.SaveChanges();
        }
    }
}
